var t = require("../../utils/public");
var app = getApp();
var status = require('../../utils/index.js');
var util = require('../../utils/util.js');

Component({
  properties: {
    spuItem: {
      type: Object,
      value: {
        spuId: "",
        skuId: "",
        spuImage: "",
        spuName: "",
        endTime: 0,
        beginTime: "",
        actPrice: ["", ""],
        marketPrice: ["", ""],
        spuCanBuyNum: "",
        soldNum: "",
        actId: "",
        limitMemberNum: "",
        limitOrderNum: "",
        serverTime: "",
        isLimit: false,
        skuList: [],
        spuDescribe: "",
        is_take_fullreduction: 0,
        bigImg: '',
        car_count: 0
      },
      observer: function (t) {
        let url = `/lionfish_comshop/pages/goods/goodsDetail?&id=${t.actId}`;
        this.setData({ url });
      }
    },
    isPast: {
      type: Boolean,
      value: false
    },
    actEnd: {
      type: Boolean,
      value: false
    },
    reduction: {
      type: Object,
      value: {
        full_money: '',
        full_reducemoney: '',
        is_open_fullreduction: 0
      }
    },
    isShowListCount: {
      type: Number,
      value: 0
    },
    shopType: {
        type: Number,
        value: 0,
        observer: function (t) {
            // console.log(t);
        }
    },
    activeOn: {
        type: Number,
        value: 0,
        observer: function (e) {
            // console.log(e);
        }
    },
    changeCarCount: {
      type: Boolean,
      value: false,
      observer: function (t) {
        console.log(t);
        if (t) this.setData({ number: this.data.spuItem.car_count || 0 });
      }
    },
    needAuth: {
      type: Boolean,
      value: false
    },
    is_open_vipcard_buy: {
      type: Number,
      value: 0
    },
    canLevelBuy: {
      type: Boolean,
      value: false
    },
    showPickTime: {
      type: Boolean,
      value: false
    },
    skin: {
      type: Object
    },
    saleUnit: {
      type: String
    },
    iconBg: {
      type: String
    }
  },
  attached() {
    this.setData({ placeholdeImg: app.globalData.placeholdeImg})
  },
  data: {
    disabled: false,
    placeholdeImg: '',
    number: 0,
    url: ''
  },
  ready: function () {
    this.setData({
      number: this.data.spuItem.car_count || 0
    });
  },
  methods: {

    /*playVideo: function (e) {

      console.log('视频id',e);

      var _index = e.currentTarget.dataset.id;//视频id

      this.setData({
        _index: _index, //把id赋值给id
      })

      //停止正在播放的视频
      var videoContextPrev = wx.createVideoContext(this.data._index);
      videoContextPrev.stop();

      setTimeout(function() {
        //将点击视频进行播放
        var videoContext = wx.createVideoContext(_index)
        videoContext.play();
      }, 500);

    },*/

    playVideo: function (e) {
      console.log(e);
      var id = e.currentTarget.dataset.id,that=this;
      var up = "spuItem.showVideo";
      that.setData({
        [up]: false
      })
      this.videoContext = wx.createVideoContext(id);
      this.videoContext.play();
    },



    videEndPlay:function(e){
      var index = e.currentTarget.dataset.index;
      var up = "spuItem.showVideo";
      this.setData({
        [up]: false
      })
    },


    goDetail: function (e) {
      let idx = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/lionfish_comshop/pages/goods/goodsDetail?&id=' + idx,
      })
    },
    openSku: function () {
      if (this.data.needAuth) {
        this.triggerEvent("authModal", true);
        return;
      }
      console.log('step1');
      this.setData({
        stopClick: true,
        disabled: false
      })
      let spuItem = this.data.spuItem;
      if (spuItem.skuList.length === void 0) {
        this.triggerEvent("openSku", {
          actId: spuItem.actId,
          skuList: spuItem.skuList,
          promotionDTO: spuItem.promotionDTO || '',
          is_take_vipcard: spuItem.is_take_vipcard,
          is_mb_level_buy: spuItem.is_mb_level_buy,
          allData: {
            spuName: spuItem.spuName,
            skuImage: spuItem.skuImage,
            actPrice: spuItem.actPrice,
            canBuyNum: spuItem.spuCanBuyNum,
            stock: spuItem.spuCanBuyNum,
            marketPrice: spuItem.marketPrice
          }
        });
      } else {
        this.addCart({ value: 1, type: "plus" });
      }
    },
      addCartYs: function () {
          this.addCart({ value: 1, type: "plus" });
      },
    countDownEnd: function () {
      this.setData({
        actEnd: true
      });
    },
    submit2: function (e) {
      (0, t.collectFormIds)(e.detail.formId);
    },
    changeNumber: function (t) {
      var e = t.detail;
      e && this.addCart(e);
    },
    outOfMax: function (t) {
      var e = t.detail, canBuyNum = this.data.spuItem.spuCanBuyNum;
      if (this.data.number >= canBuyNum) {
        wx.showToast({
          title: "不能购买更多啦",
          icon: "none"
        })
      }
    },
      toTypePage: function (t) {
          var id = t.currentTarget.dataset.id, type = t.currentTarget.dataset.type, name = t.currentTarget.dataset.name;
          app.globalData.typeCateId = id;
          if(type != 1){
              wx.switchTab({
                  url: '/lionfish_comshop/pages/type/index'
              })
          }else{
              this.triggerEvent("goNavUrlOther", { idx: id, name: name});
            //   this.selectComponent('#myTabs').getNode();
          }
      },
      addCart: function (t) {
          // {value: 2, type: "plus/minus"}
          var token = wx.getStorageSync('token');
          var community = wx.getStorageSync('community');
          var goods_id = this.data.spuItem.actId;
          var community_id = community.communityId;
          let that = this;
          if (t.type == 'plus') {
              let data = {
                  goods_id,
                  community_id,
                  quantity: 1,
                  sku_str: '',
                  buy_type: 'dan',
                  pin_id: 0,
                  is_just_addcar: 1
              }
              util.addCart(data).then(res => {
                  if (res.showVipModal == 1) {
                      let { pop_vipmember_buyimage } = res.data;
                      that.triggerEvent("vipModal", { pop_vipmember_buyimage, showVipModal: true, visible: false });
                  } else {
                      if (res.data.code == 3) {
                          let max_quantity = res.data.max_quantity || '';
                          (max_quantity > 0) && that.setData({ number: that.data.number })
                          wx.showToast({
                              title: res.data.msg,
                              icon: 'none',
                              duration: 2000
                          })
                      } else if (res.data.code == 4) {
                          that.setData({ needAuth: true })
                          that.triggerEvent("authModal", true);
                      } else if (res.data.code == 6 || res.data.code == 7) {
                          let max_quantity = res.data.max_quantity || '';
                          (max_quantity > 0) && that.setData({ number: that.data.number })
                          var msg = res.data.msg;
                          wx.showToast({
                              title: msg,
                              icon: 'none',
                              duration: 2000
                          })
                      } else {
                          that.triggerEvent("changeCartNum", res.data.total);
                          that.setData({ number: res.data.cur_count })
                          //wx.showToast({
                              //title: "已加入购物车",
                              //image: "../../images/addShopCart.png"
                          //})
                          status.indexListCarCount(goods_id, res.data.cur_count);
                      }
                  }
              })
          } else {
              app.util.request({
                  url: 'entry/wxapp/user',
                  data: {
                      controller: 'car.reduce_car_goods',
                      token: token,
                      goods_id: goods_id,
                      community_id: community_id,
                      quantity: 1,
                      sku_str: '',
                      buy_type: 'dan',
                      pin_id: 0,
                      is_just_addcar: 1
                  },
                  dataType: 'json',
                  method: 'POST',
                  success: function (res) {
                      if (res.data.code == 3) {
                          wx.showToast({
                              title: res.data.msg,
                              icon: 'none',
                              duration: 2000
                          })
                      } else if (res.data.code == 4) {
                          if (that.data.needAuth) {
                              that.setData({ needAuth: true })
                              that.triggerEvent("authModal", true);
                              return;
                          }
                      } else {
                          that.triggerEvent("changeCartNum", res.data.total);
                          that.setData({ number: res.data.cur_count })
                          status.indexListCarCount(goods_id, res.data.cur_count);
                      }
                  }
              })
          }

      }
  }
});